home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programmer's Power Pack / Delphi Volume 1.iso / s_to_z / spidr100 / setup.arv / STKTEST.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  3.7 KB  |  162 lines

  1. {----------------------------------------------------------------------------
  2. |
  3. | Library: Spider Containers for Object Pascal
  4. |
  5. | Module: StkTest.Pas
  6. |
  7. | Description: Form for TStack test.
  8. |
  9. | History: Version 1.0  March 1996. Copyright (c) 1996 Michel Brazeau
  10. |                                   Interval Software
  11. |
  12. |---------------------------------------------------------------------------}
  13. unit StkTest;
  14.  
  15. interface
  16.  
  17. uses
  18.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  19.   Forms, Dialogs, StdCtrls,
  20.  
  21.   ObjStack;   { TStack }
  22.  
  23. type
  24.   TStackForm = class(TForm)
  25.     PushButton: TButton;
  26.     PopButton: TButton;
  27.     ClearButton: TButton;
  28.     ItemCount: TLabel;
  29.     ListBox: TListBox;
  30.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  31.     procedure FormCreate(Sender: TObject);
  32.     procedure FormDestroy(Sender: TObject);
  33.     procedure PushButtonClick(Sender: TObject);
  34.     procedure PopButtonClick(Sender: TObject);
  35.     procedure ClearButtonClick(Sender: TObject);
  36.   private
  37.      { Private declarations }
  38.      Stack : TStack;
  39.  
  40.      { redraws the list box, from the contents of the stack }
  41.      procedure UpdateListBox;
  42.  
  43.      { iterator method to add a TStringCombo to the list box }
  44.      procedure AddString(const Obj : TObject);
  45.  
  46.   public
  47.     { Public declarations }
  48.   end;
  49.  
  50. {--------------------------------------------------------------------------}
  51.  
  52. implementation
  53.  
  54. {$R *.DFM}
  55.  
  56. uses
  57.      ObjBuckt;   { TStringCombo }
  58.  
  59. {--------------------------------------------------------------------------}
  60.  
  61. procedure TStackForm.FormClose(Sender: TObject; var Action: TCloseAction);
  62. begin
  63.     Action := caFree;
  64. end;
  65.  
  66. {--------------------------------------------------------------------------}
  67.  
  68. procedure TStackForm.FormCreate(Sender: TObject);
  69. begin
  70.     Stack := TStack.Create(TStringCombo);
  71. end;
  72.  
  73. {--------------------------------------------------------------------------}
  74.  
  75. procedure TStackForm.FormDestroy(Sender: TObject);
  76. begin
  77.     Stack.Free;
  78. end;
  79.  
  80. {--------------------------------------------------------------------------}
  81.  
  82. procedure TStackForm.UpdateListBox;
  83. begin
  84.     ListBox.Clear;
  85.  
  86.     Screen.Cursor := crHourGlass;
  87.  
  88.     ListBox.Enabled := False;
  89.  
  90.     try
  91.  
  92.         Stack.ForEachCallMethod(AddString);
  93.  
  94.     finally
  95.  
  96.         ListBox.Enabled := True;
  97.         Screen.Cursor := crDefault;
  98.  
  99.     end;
  100.  
  101.     ItemCount.Caption := IntToStr(Stack.Size);
  102. end;
  103.  
  104. {--------------------------------------------------------------------------}
  105.  
  106. procedure TStackForm.AddString(const Obj : TObject);
  107. begin
  108.     ListBox.Items.Add((Obj as TStringCombo).Str);
  109. end;
  110.  
  111. {--------------------------------------------------------------------------}
  112.  
  113. procedure TStackForm.PushButtonClick(Sender: TObject);
  114. const
  115.     Str         : String     = '';
  116.  
  117. var
  118.     StringCombo : TStringCombo;
  119.  
  120. begin
  121.     if not InputQuery('', 'String to push: ', Str) then
  122.         Exit;
  123.  
  124.     StringCombo := TStringCombo.Create(Str, nil);
  125.     try
  126.         Stack.Push(StringCombo);
  127.     except
  128.         StringCombo.Free;
  129.     end;
  130.  
  131.     UpdateListBox;
  132. end;
  133.  
  134. {--------------------------------------------------------------------------}
  135.  
  136. procedure TStackForm.PopButtonClick(Sender: TObject);
  137. var
  138.     StringCombo : TStringCombo;
  139.  
  140. begin
  141.     StringCombo := Stack.Pop as TStringCombo;
  142.  
  143.     MessageDlg(StringCombo.Str + ' popped.', mtInformation, [mbOk], 0);
  144.  
  145.     StringCombo.Free;
  146.  
  147.     UpdateListBox;
  148. end;
  149.  
  150. {--------------------------------------------------------------------------}
  151.  
  152. procedure TStackForm.ClearButtonClick(Sender: TObject);
  153. begin
  154.     Stack.Clear;
  155.  
  156.     UpdateListBox;
  157. end;
  158.  
  159. {--------------------------------------------------------------------------}
  160.  
  161. end.
  162.